package com.gxar.quick.ar.server.base.common.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class TextGradientGenerator {

    public static void main(String[] args) {
        generateTextWithGradient("Aa", "Alimama DaoLiTi", Font.BOLD, 40, Color.ORANGE, Color.RED, "text_with_gradient.png");
    }

    public static void generateTextWithGradient(String text, String fontName, int fontStyle, int fontSize,
            Color startColor, Color endColor, String outputFileName) {
        int width = 78;
        int height = 78;

        // 创建带有透明背景的 BufferedImage
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();

        // 设置抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 设置字体样式
        Font font = new Font(fontName, fontStyle, fontSize);
        g2d.setFont(font);

        // 创建渐变画笔
        GradientPaint gradient = new GradientPaint(0, 0, startColor, 0, 78-40, endColor);
        g2d.setPaint(gradient);

        // 在指定位置绘制文字
        int x = 0;
        int y = 40;
        g2d.drawString(text, x, y);

        // 释放资源
        g2d.dispose();

        // 保存生成的图像到文件
        try {
            ImageIO.write(image, "png", new File(outputFileName));
            System.out.println("Text with gradient image saved to: " + outputFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
